package com.peng.egmvc1.controller;

import com.peng.egmvc1.mapper.AddressMapper;
import com.peng.egmvc1.pojo.dto.InsertAddressDTO;
import com.peng.egmvc1.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地址Controller
 * @Author peng
 * @Date 2024-04-22 19:09
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/insert")
    public String insertAddress(InsertAddressDTO addressDTO){
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        int i = addressMapper.insertAddress(address);
        if (i > 0){
            return "insert success";
        }
        return "insert error";
    }

    @GetMapping("/select")
    public List<Address> selectAllAddress(){
        return addressMapper.selectAllAddress();
    }

    @PostMapping("/delete")
    public String deleteAddressById(Integer id){
        int i = addressMapper.deleteAddressById(id);
        if (i > 0){
            return "delete success";
        }
        return "delete error";
    }

    @PostMapping("/update")
    public String updateAddressById(Address address){
        int i = addressMapper.updateAddress(address);
        if (i > 0){
            return "update success";
        }
        return "update error";
    }
}
